using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using Microsoft.AceEngineering.SPM.SPMBusinessLayer;
using Microsoft.AceEngineering.SPM.SPMCustomDataEntity;


public partial class Admin_Workflow_TaskMapping : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
            ValidateUser();
        else
        {
            UserCDE userData = (UserCDE)Session["user"];
            if (userData.Time.AddMinutes(Int32.Parse(ConfigurationManager.AppSettings["AbsoluteExpiration"].ToString())) <= DateTime.Now)
                ValidateUser();
        }

        bool isAdmin = UsersBL.GetUserAdmin(Session["loginname"].ToString());
        if (!isAdmin)
            Response.Redirect("~/Home.aspx");

        LoadScoreRanges();
    }

    private string GiveMeAlias(string domainAndAlias)
    {
        domainAndAlias = domainAndAlias.ToLower();
        char[] strSeperator = new char[] { '\\' };
        string[] aryAuthUser = new string[2];
        aryAuthUser = domainAndAlias.Split(strSeperator);
        return aryAuthUser[1];
    }

    private void ValidateUser()
    {
        string currAlias = GiveMeAlias(HttpContext.Current.User.Identity.Name);
        UserCDE user = UsersBL.ValidateUser(currAlias);
        if (user.Active)
        {
            Session["user"] = user;
            Session["loginname"] = user.UserAlias;
        }
        else
        {
            Response.Redirect(Request.ApplicationPath + "/Errors/NoAccess.htm");
        }
    }

    protected void LoadScoreRanges()
    {
        if (ddlScoreRange.Items.Count == 0)
        {
            ddlScoreRange.DataSource = ScoreRangeTaskBL.dsLoadDataSet("SPM_spGetScoreRangesList");
            ddlScoreRange.DataBind();
        }

        if (ddlScoreRange.Items.Count == 0)
        {
            pnlError.Visible = true;
            pnlGrid.Visible = false;
            ddlScoreRange.DataSource = ScoreRangeTaskBL.dsLoadDataSet("SPM_spGetScoreRangesList");
            ddlScoreRange.DataBind();
        }
        else
        {
            pnlError.Visible = false;
            pnlGrid.Visible = true;
        }
    }

    protected void gridTaskMappingEnabled_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        gridTaskMappingDisabled.DataBind();
    }

    protected void gridTaskMappingDisabled_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        gridTaskMappingEnabled.DataBind();
    }

    protected void gridTaskMappingEnabled_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (ddlScoreRange.Items.Count > 0)
        {
            e.InputParameters[0] = Int32.Parse(ddlScoreRange.SelectedValue);
        }
        else
        {
            e.Cancel = true;
        }
    }

    protected void gridTaskMappingDisabled_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (ddlScoreRange.Items.Count > 0)
        {
            e.InputParameters[0] = Int32.Parse(ddlScoreRange.SelectedValue);
        }
        else
        {
            e.Cancel = true;
        }
    }

    protected void gridTaskMappingEnabled_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int scoreID = Int32.Parse(ddlScoreRange.SelectedValue);
        string taskID = gridTaskMappingEnabled.DataKeys[e.RowIndex].Value.ToString();

        try
        {
            ScoreRangeTaskBL.DeleteMapping(scoreID, taskID);
        }
        catch (Exception ex)
        {
            string strErrorType = ex.Message.ToString().Substring(0, 8);

            Page pge = (Page)this.Page;
            Microsoft.ACEEngineering.Utility.CommonProcess.APMErrorHandling(ex, Request.IsLocal, pge, Session, Request);
            if (!Request.IsLocal && strErrorType != "APMError")
            {
                Server.ClearError();
                Server.Transfer(Request.ApplicationPath + "/Errors/GenericErrorPage.htm");
            }
        }
    }

    protected void gridTaskMappingDisabled_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int scoreID = Int32.Parse(ddlScoreRange.SelectedValue);
        string taskID = gridTaskMappingDisabled.DataKeys[e.RowIndex].Value.ToString();

        try
        {
            ScoreRangeTaskBL.CreateMapping(scoreID, taskID, Session["loginname"].ToString());
        }
        catch (Exception ex)
        {
            string strErrorType = ex.Message.ToString().Substring(0, 8);

            Page pge = (Page)this.Page;
            Microsoft.ACEEngineering.Utility.CommonProcess.APMErrorHandling(ex, Request.IsLocal, pge, Session, Request);
            if (!Request.IsLocal && strErrorType != "APMError")
            {
                Server.ClearError();
                Server.Transfer(Request.ApplicationPath + "/Errors/GenericErrorPage.htm");
            }
        }
    }

    protected void ddlScoreRange_SelectedIndexChanged(object sender, EventArgs e)
    {
        gridTaskMappingEnabled.PageIndex = 0;
        gridTaskMappingEnabled.DataBind();
        gridTaskMappingDisabled.PageIndex = 0;
        gridTaskMappingDisabled.DataBind();
    }

    protected void gridTaskMappingEnabled_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        foreach (System.Web.UI.Control BaseControl in e.Row.Cells[0].Controls)
        {
            if (BaseControl.GetType().BaseType == typeof(LinkButton))
            {
                LinkButton linkBtn = (LinkButton)BaseControl;
                if (linkBtn.CommandName == "Delete")
                    linkBtn.OnClientClick = "return confirm('Are you sure you want to remove this Score Range - Task Mapping?');";
            }
        }
    }
}
